<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>泛型(generic) | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="c#学习笔记" />
  
  
  
  
  <meta name="description" content="泛型(generic)">
<meta name="keywords" content="c#学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="泛型(generic)">
<meta property="og:url" content="http://www.hais2.com/2015/05/22/c#-generic/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="泛型(generic)">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp65.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp66.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp67.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp68.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp69.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp70.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp71.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp72.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp73.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp74.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp78.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp79.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp80.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp81.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp82.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp83.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp84.png">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="泛型(generic)">
<meta name="twitter:description" content="泛型(generic)">
<meta name="twitter:image" content="http://www.hais2.com/image/cSharp/cSharp65.png">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-c#-generic" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      泛型(generic)
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2015/05/22/c#-generic/" class="article-date">
	  <time datetime="2015-05-22T08:17:54.000Z" itemprop="datePublished">May 22, 2015</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="什么是泛型"><a href="#什么是泛型" class="headerlink" title="什么是泛型"></a>什么是泛型</h2><p>　　我们平常所用到的类型都是特定的类型——由编程语言或者BCL(基类库)、程序员自己定义的。为了让代码更加的灵活、解耦，我们可以将类的行为提取出来，使这些相似性的特征不仅可以用在当前的数据类型上，而且还可以扩展到其他的类型上，这样使类更加的有用。<br>　　泛型就是专门处理这种情况的，泛型就可以替代这种抽象的数据类型，泛型就相当于是类型的占位符。<a id="more"></a></p>
<h3 id="示例："><a href="#示例：" class="headerlink" title="示例："></a>示例：</h3><p>　　假设我们声明了一个<code>MyIntStack</code>类，该类实现了一个int类型的栈，它允许我们把int类型的数据压栈和出栈。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyIntStack</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> StackPointer=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span>[] StackArray;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Push</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//压栈  </span></span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">pop</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//出栈</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　假设我们需要一个float类型的值也具有相同的功能(压栈和出栈),我们需要重新编写针对float类型的代码：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyIntStack</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> StackPointer=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">float</span>[] StackArray;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Push</span><span class="params">(<span class="keyword">float</span> x)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//压栈  </span></span><br><span class="line">    &#125;   </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">float</span> <span class="title">pop</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//出栈</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　※ 如果我们需要其他类型的数据也具有压栈和出栈的功能，就前面的做法，需要针对每种类型编写相似的代码，这些亢余代码不仅占据了额外的空间，而且不利于调试和维护。</p>
<h2 id="c-中的泛型"><a href="#c-中的泛型" class="headerlink" title="c#中的泛型"></a>c#中的泛型</h2><p>　　在c#2.0中，微软引入了泛型特性。泛型允许我们声明类型参数化的代码，我们可以使用“<strong>类型占位符</strong>”写代码，然后在创建类的实例的时候提供真实的类型。<br><img src="/image/cSharp/cSharp65.png" alt=""><br>　　★ c#提供了5种泛型：类、结构、接口、委托、方法。其中前四个是类型，而方法是成员。</p>
<h3 id="栈的示例"><a href="#栈的示例" class="headerlink" title="栈的示例"></a>栈的示例</h3><p>　　我们接着前面的代码，使用泛型处理代码亢余的情况。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyStack</span>&lt;T&gt;</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> StackPointer=<span class="number">0</span>;</span><br><span class="line">    T[] StackArray;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Push</span><span class="params">(T x)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//压栈</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">Pop</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//出栈</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　以上是一个泛型类的声明，“T”代表类型的占位符(也可以是其他字符)，在实例化时，每一个“T”都会被编译器替换为实际类型。</p>
<h2 id="泛型类"><a href="#泛型类" class="headerlink" title="泛型类"></a>泛型类</h2><p>　　创建和使用常规的非泛型的类有两个步骤：声明类、创建类的实例。但是泛型类不是实际的类，而是类的模板，创建时需要：<br>　　■ 在某些类型上使用占位符来声明一个泛型类。<br>　　■ 为占位符提供真实类型，这样就有了真实类型的定义，填补了所有的“空缺”。<br>　　■ 从“填空后”的类定义创建实例。<br><img src="/image/cSharp/cSharp66.png" alt=""></p>
<h3 id="声明泛型类"><a href="#声明泛型类" class="headerlink" title="声明泛型类"></a>声明泛型类</h3><p>　　声明泛型类：<br>　　■ 在类名之后放置一组尖括号。<br>　　■ 在尖括号中用逗号分隔的占位符字符串来表示希望提供的类型。这叫做<em>类型参数(type parameter)</em>。<br>　　■ 在泛型类声明的主体中使用类型参数来表示应该被替代的类型。<br><figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//类型参数</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SomeClass</span> &lt;<span class="title">T1</span>,<span class="title">T2</span>&gt;</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//通常在这些位置使用泛型类型</span></span><br><span class="line">    <span class="keyword">public</span> T1 SomeVar = <span class="keyword">new</span> <span class="type">T1</span>();</span><br><span class="line">    <span class="keyword">public</span> T2 OtherVar = <span class="keyword">new</span> <span class="type">T2</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="创建构造类型"><a href="#创建构造类型" class="headerlink" title="创建构造类型"></a>创建构造类型</h3><p>　　我们不能直接从泛型类型创建类对象。<br>　　首先，我们需要告诉编译器使用哪些真实的类型来替代占位符(类型参数)，编译器获取这些真实类型并从它创建一个真实的类型对象。<br>　　★ 要从泛型类构建类类型，列出类名字并在尖括号中提供真实类型来替代类型参数，要替代类型参数的真实类型叫做<em>类型实参(type argument)</em>。<br><img src="/image/cSharp/cSharp67.png" alt=""><br>　　★ 为泛型类提供了类型实参，替换泛型类主体中的相应的类型参数，这时相当于产生了一个构造类型，从这个构造类可以创建真实类型的实例了。<br><img src="/image/cSharp/cSharp68.png" alt=""><br>　　类型参数和类型实参的区别：<br><img src="/image/cSharp/cSharp69.png" alt=""></p>
<h3 id="创建变量和实例"><a href="#创建变量和实例" class="headerlink" title="创建变量和实例"></a>创建变量和实例</h3><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//普通类对象的创建</span></span><br><span class="line">MyNonGenClass myNGC = <span class="keyword">new</span> MyNonGenClass();</span><br><span class="line"></span><br><span class="line"><span class="comment">//泛型类对象的创建吗，使用short和int类型进行实例化。</span></span><br><span class="line">SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt; mySc1 = <span class="keyword">new</span> SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt;();</span><br><span class="line"></span><br><span class="line"><span class="comment">//和第二行代码一样，区别是使用var关键字使编译器使用类型。</span></span><br><span class="line"><span class="keyword">var</span> mySc2 = <span class="keyword">new</span> SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt;();</span><br></pre></td></tr></table></figure>
<p>　　泛型类也可以引用和实例分开创建。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//泛型类声明</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SomeClass</span> &lt;T1,T2&gt;</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> T1 SomeVar;</span><br><span class="line">    <span class="keyword">public</span> T2 OtherVar;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt; myInst; <span class="comment">//创建引用</span></span><br><span class="line">myInst = <span class="keyword">new</span> SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt;();  <span class="comment">//创建实例</span></span><br></pre></td></tr></table></figure></p>
<p>　　■ <code>SomeClass&lt;short,int&gt; myInst</code>这句代码在栈上为<code>myInst</code>分配了一个引用，值是null。<br>　　■ <code>myInst = new SomeClass&lt;short,int&gt;()</code>这句代码在堆上分配实例，并且把引用赋值给变量。</p>
<p>　　★ 可以从同一个泛型类型构建出不同的类类型。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SomeClass</span> &lt;T1,T2&gt;</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">public</span> T1 SomeVar;</span><br><span class="line">    <span class="keyword">public</span> T2 OtherVar;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//从同一个泛型类型构建出不同的类对象</span></span><br><span class="line">        var class1 = <span class="keyword">new</span> SomeClass&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt;(); </span><br><span class="line">        var class2 = <span class="keyword">new</span> SomeClass&lt;<span class="keyword">int</span>,<span class="keyword">long</span>&gt;();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp70.png" alt=""></p>
<h2 id="类型参数的约束"><a href="#类型参数的约束" class="headerlink" title="类型参数的约束"></a>类型参数的约束</h2><p>　　有时候我们并不能让任何类型的类型实参去匹配类型参数，也就是说需要取代“T”占位符的参数类型有时候需要有一些限制，并不是所有的代码中都需要所有的数据类型。<br>　　★ 要让泛型变的更加准确，我们需要提供额外的信息让编译器直达参数可以接受哪些类型，这些额外的信息叫做<strong>约束(constrain)</strong>，只有符合约束的实参才能用于类型参数。</p>
<h3 id="where子句"><a href="#where子句" class="headerlink" title="where子句"></a>where子句</h3><p>　　约束使用where子句列出：<br>　　■ 每一个有约束的类型参数有自己的where子句。<br>　　■ 如果形参有多个约束，它们在where子中使用逗号分隔。<br><img src="/image/cSharp/cSharp71.png" alt=""><br>　　where子句要点：<br>　　■ 它们在类型参数列表的关闭尖括号之后列出。<br>　　■ 它们不使用逗号或其他符号分隔。<br>　　■ 它们可以以任何次序列出。<br><img src="/image/cSharp/cSharp72.png" alt=""></p>
<h3 id="约束类型和次序"><a href="#约束类型和次序" class="headerlink" title="约束类型和次序"></a>约束类型和次序</h3><p><img src="/image/cSharp/cSharp73.png" alt=""><br>　　where子句可以以任何次序列出，但是where子句中的约束却必须有特定的顺序。<br>　　■ 最多只能由一个主约束，而且必须放在第一位。<br>　　■ 可以有任意多的<em>InterfaceName</em>约束。<br>　　■ 如果存在构造函数约束(<em>new()</em>)，则必须放在最后。<br><img src="/image/cSharp/cSharp74.png" alt=""><br><strong>示例</strong>：<br><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SortedList</span>&lt;S&gt;</span></span><br><span class="line">        where <span class="symbol">S:</span>IComparable&lt;S&gt;</span><br><span class="line">    &#123;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Linkedist</span>&lt;M,<span class="title">N</span>&gt;</span></span><br><span class="line">        where <span class="symbol">M:</span>IComparable&lt;S&gt;</span><br><span class="line">        where <span class="symbol">N:</span>ICloneable</span><br><span class="line">    &#123;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyDictionary</span>&lt;KeyType,<span class="title">ValueType</span>&gt;</span></span><br><span class="line">        where <span class="symbol">KeyType:</span>IEnumerable,new()</span><br><span class="line">    &#123;</span><br><span class="line">        ...</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="泛型结构"><a href="#泛型结构" class="headerlink" title="泛型结构"></a>泛型结构</h2><p>　　与泛型类相似，泛型结构可以有类型参数和约束。<strong>泛型结构的规则和条件与泛型类是一样的</strong>。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> intData = <span class="keyword">new</span> ShowData&lt;<span class="keyword">int</span>&gt;(<span class="number">10</span>);</span><br><span class="line">        <span class="keyword">var</span> stringData = <span class="keyword">new</span> ShowData&lt;<span class="keyword">string</span>&gt;(<span class="string">"struct"</span>);</span><br><span class="line">        Console.WriteLine(intData.Data);</span><br><span class="line">        Console.WriteLine(stringData.Data);</span><br><span class="line">        Console.ReadKey();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> ShowData&lt;T&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">public</span> T Data &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">ShowData</span> (<span class="params">T <span class="keyword">value</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Data = <span class="keyword">value</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp78.png" alt=""></p>
<h2 id="泛型接口"><a href="#泛型接口" class="headerlink" title="泛型接口"></a>泛型接口</h2><p>　　泛型接口允许我们编写<em>参数</em>和<em>成员返回类型</em>是<em>泛型类型参数</em>的接口。</p>
<h3 id="声明泛型接口示例"><a href="#声明泛型接口示例" class="headerlink" title="声明泛型接口示例"></a>声明泛型接口示例</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//参数和成员的返回类型都是泛型T</span></span><br><span class="line">interface IMyIfc&lt;T&gt;  </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">T <span class="title">ReturenIt</span><span class="params">(T inValue)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//泛型类实现泛型接口，非泛型类也可实现泛型接口</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Simple</span>&lt;S&gt; :</span> IMyIfc&lt;S&gt;  </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> S <span class="title">ReturnIt</span><span class="params">(S inValue)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> inValue;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//两种不同的参数类型</span></span><br><span class="line">        var trivInt = <span class="keyword">new</span> Simple&lt;<span class="keyword">int</span>&gt;();</span><br><span class="line">        var trivString = <span class="keyword">new</span> Simple&lt;<span class="built_in">string</span>&gt;();</span><br><span class="line">        </span><br><span class="line">        Console.WriteLine(trivInt.ReturnIt(<span class="number">5</span>));</span><br><span class="line">        Console.WriteLine(trivString.ReturnIt(<span class="string">"泛型接口"</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>###　使用泛型接口示例<br>　　■ 与其他的泛型相似，同一个接口，实现了不同<em>类型参数</em>就是不同的接口(比如<code>IIfc&lt;int&gt;</code>和<code>IIfc&lt;string&gt;</code>虽然是同一个接口，但是由于类型参数不同，就是不同的接口)。<br>　　■ 可以在非泛型类中实现泛型接口。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">interface IMyIfc&lt;T&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">T <span class="title">ReturnIt</span><span class="params">(T inValue)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//源于同一个的泛型接口的两个不同接口</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Simple</span> :</span> IMyIfc&lt;<span class="keyword">int</span>&gt;,IMyIfc&lt;<span class="built_in">string</span>&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//实现int类型接口</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">ReturnIt</span><span class="params">(<span class="keyword">int</span> inValue)</span>  </span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> inValue;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//实现string类型接口</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="built_in">string</span> <span class="title">ReturnIt</span><span class="params">(<span class="built_in">string</span> inValue)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> inValue;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Program</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span><span class="params">(<span class="built_in">string</span>[] args)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//两种不同的参数类型</span></span><br><span class="line">        var trivInt = <span class="keyword">new</span> Simple&lt;<span class="keyword">int</span>&gt;();</span><br><span class="line">        var trivString = <span class="keyword">new</span> Simple&lt;<span class="built_in">string</span>&gt;();</span><br><span class="line">        </span><br><span class="line">        Console.WriteLine(trivInt.ReturnIt(<span class="number">5</span>));</span><br><span class="line">        Console.WriteLine(trivString.ReturnIt(<span class="string">"泛型接口"</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="泛型接口的实现必须唯一"><a href="#泛型接口的实现必须唯一" class="headerlink" title="泛型接口的实现必须唯一"></a>泛型接口的实现必须唯一</h3><p>　　在实现泛型接口时，必须确定的是不能让一个类去实现两个相同的接口，这是必须避免发生的，以下代码展示了这种情况的例子：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">interface IMyIfc&lt;T&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">T <span class="title">ReturnIt</span><span class="params">(T inValue)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//错误的代码，因为如果泛型“S”也是int类型，那么就相当于Simple类实现了两个相同的IMyIfc接口。</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Simple</span>&lt;S&gt; :</span> IMyIfc&lt;<span class="keyword">int</span>&gt;,IMyIfc&lt;S&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//实现第一个接口</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">ReturnIt</span><span class="params">(<span class="keyword">int</span> inValue)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> inValue;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//实现第二个接口，如果它是int类型，就会出现错误异常</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> S <span class="title">ReturnIt</span><span class="params">(S inValue)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> inValue;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　<strong>泛型接口的名字不会和非泛型冲突，比如</strong><code>IMyIfc&lt;T&gt;</code><strong>和</strong><code>IMyIfc</code><strong>是不会有命名冲突的。</strong></p>
<h2 id="泛型委托"><a href="#泛型委托" class="headerlink" title="泛型委托"></a>泛型委托</h2><p><img src="/image/cSharp/cSharp79.png" alt=""><br>　　■ 要声明泛型委托，在委托名称和委托参数列表之间的尖括号中放类型参数列表。<br>　　■ 这里有两个参数列表，委托形参列表和类型参数列表<br>　　■ 类型参数的范围包括：<br>　　　　□ 返回值<br>　　　　□ 形参列表<br>　　　　□ 约束子句<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delegate</span> <span class="keyword">void</span> MyDeledate&lt;T&gt;(T <span class="keyword">value</span>);   <span class="comment">//泛型委托</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title">Simple</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">PrintString</span>(<span class="params"><span class="keyword">string</span> s</span>)  <span class="comment">//方法匹配委托</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Console.WriteLine(s);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">PrintUpperString</span>(<span class="params"><span class="keyword">string</span> s</span>)  <span class="comment">//方法匹配委托</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Console.WriteLine(s.ToUpper());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> myDel = <span class="keyword">new</span> MyDeledate&lt;<span class="keyword">string</span>&gt;(Simple.PrintString);  <span class="comment">//创建委托实例</span></span><br><span class="line">        myDel += Simple.PrintUpperString;   <span class="comment">//添加方法</span></span><br><span class="line">        myDel(<span class="string">"generic_delegate"</span>);            <span class="comment">//调用委托</span></span><br><span class="line">        Console.ReadKey();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<img src="/image/cSharp/cSharp80.png" alt=""></p>
<h2 id="泛型方法"><a href="#泛型方法" class="headerlink" title="泛型方法"></a>泛型方法</h2><p>　　与其他泛型不一样的是，方法是成员，不是类型。<br>　　<strong>泛型方法可以在泛型和非泛型类以及结构和接口中声明。</strong><br><img src="/image/cSharp/cSharp81.png" alt=""></p>
<h3 id="声明泛型方法"><a href="#声明泛型方法" class="headerlink" title="声明泛型方法"></a>声明泛型方法</h3><p>　　■ 泛型方法和泛型委托一样，有两个参数列表：方法参数列表、类型参数列表。<br><img src="/image/cSharp/cSharp82.png" alt=""></p>
<h3 id="调用泛型方法"><a href="#调用泛型方法" class="headerlink" title="调用泛型方法"></a>调用泛型方法</h3><p>　　要调用泛型方法，应该在方法调用时提供类型实参：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MyMethod&lt;<span class="keyword">short</span>,<span class="keyword">int</span>&gt;();</span><br><span class="line">MyMethod&lt;<span class="keyword">int</span>,<span class="keyword">long</span>&gt;();</span><br></pre></td></tr></table></figure></p>
<p>有两个实例的泛型方法：<br><img src="/image/cSharp/cSharp83.png" alt=""></p>
<h3 id="推断类型"><a href="#推断类型" class="headerlink" title="推断类型"></a>推断类型</h3><p>　　如果我们为泛型方法传入了参数，编译器可以根据传入的参数判断类型参数的类型，因此可以省略类型参数，形成简写形式：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//类型参数列表和方法参数列表都是“T”类型</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> MyMethod &lt;T&gt; (T myVal)</span><br><span class="line">&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> MyInt=<span class="number">5</span>;</span><br><span class="line">MyMethod &lt;<span class="keyword">int</span>&gt; (MyInt);  <span class="comment">//两个类型都是int类型的</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//可以简写成</span></span><br><span class="line">MyMethod (MyInt);</span><br></pre></td></tr></table></figure></p>
<h3 id="泛型方法的示例"><a href="#泛型方法的示例" class="headerlink" title="泛型方法的示例"></a>泛型方法的示例</h3><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title">Simple</span>          <span class="comment">//非泛型类</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> ReverseAndPrint&lt;T&gt;(T[] arr)  <span class="comment">//泛型方法</span></span><br><span class="line">    &#123;</span><br><span class="line">        Array.Reverse(arr);</span><br><span class="line">        <span class="keyword">foreach</span> (T item <span class="keyword">in</span> arr)</span><br><span class="line">        &#123;</span><br><span class="line">            Console.Write(<span class="string">"&#123;0&#125;，"</span>,item.ToString());</span><br><span class="line">        &#125;</span><br><span class="line">        Console.WriteLine(<span class="string">""</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> intArray = <span class="keyword">new</span> <span class="keyword">int</span>[] &#123; <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">11</span> &#125;;</span><br><span class="line">        <span class="keyword">var</span> stringArray = <span class="keyword">new</span> <span class="keyword">string</span>[] &#123; <span class="string">"壹"</span>, <span class="string">"贰"</span>, <span class="string">"叁"</span> &#125;;</span><br><span class="line">        <span class="keyword">var</span> doubleArray = <span class="keyword">new</span> <span class="keyword">double</span>[] &#123; <span class="number">1.11</span>, <span class="number">2.33</span>, <span class="number">4.44</span> &#125;;</span><br><span class="line"></span><br><span class="line">        Simple.ReverseAndPrint&lt;<span class="keyword">int</span>&gt;(intArray);   <span class="comment">//调用方法</span></span><br><span class="line">        Simple.ReverseAndPrint(intArray);        <span class="comment">//简写</span></span><br><span class="line">        </span><br><span class="line">        Simple.ReverseAndPrint&lt;<span class="keyword">string</span>&gt;(stringArray);   <span class="comment">//调用方法</span></span><br><span class="line">        Simple.ReverseAndPrint(stringArray);        <span class="comment">//简写</span></span><br><span class="line"></span><br><span class="line">        Simple.ReverseAndPrint&lt;<span class="keyword">double</span>&gt;(doubleArray);   <span class="comment">//调用方法</span></span><br><span class="line">        Simple.ReverseAndPrint(doubleArray);        <span class="comment">//简写</span></span><br><span class="line"></span><br><span class="line">        Console.ReadKey();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果：<br><img src="/image/cSharp/cSharp84.png" alt=""></p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/c/">c#</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c-学习笔记/">c#学习笔记</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/05/24/c#-struct/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          结构(struct)
        
      </div>
    </a>
  
  
    <a href="/2015/05/21/c#-Event/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">事件(Event)</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是泛型"><span class="nav-number">1.</span> <span class="nav-text">什么是泛型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#示例："><span class="nav-number">1.1.</span> <span class="nav-text">示例：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#c-中的泛型"><span class="nav-number">2.</span> <span class="nav-text">c#中的泛型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#栈的示例"><span class="nav-number">2.1.</span> <span class="nav-text">栈的示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型类"><span class="nav-number">3.</span> <span class="nav-text">泛型类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明泛型类"><span class="nav-number">3.1.</span> <span class="nav-text">声明泛型类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建构造类型"><span class="nav-number">3.2.</span> <span class="nav-text">创建构造类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建变量和实例"><span class="nav-number">3.3.</span> <span class="nav-text">创建变量和实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型参数的约束"><span class="nav-number">4.</span> <span class="nav-text">类型参数的约束</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#where子句"><span class="nav-number">4.1.</span> <span class="nav-text">where子句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#约束类型和次序"><span class="nav-number">4.2.</span> <span class="nav-text">约束类型和次序</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型结构"><span class="nav-number">5.</span> <span class="nav-text">泛型结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型接口"><span class="nav-number">6.</span> <span class="nav-text">泛型接口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明泛型接口示例"><span class="nav-number">6.1.</span> <span class="nav-text">声明泛型接口示例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#泛型接口的实现必须唯一"><span class="nav-number">6.2.</span> <span class="nav-text">泛型接口的实现必须唯一</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型委托"><span class="nav-number">7.</span> <span class="nav-text">泛型委托</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#泛型方法"><span class="nav-number">8.</span> <span class="nav-text">泛型方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明泛型方法"><span class="nav-number">8.1.</span> <span class="nav-text">声明泛型方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#调用泛型方法"><span class="nav-number">8.2.</span> <span class="nav-text">调用泛型方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#推断类型"><span class="nav-number">8.3.</span> <span class="nav-text">推断类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#泛型方法的示例"><span class="nav-number">8.4.</span> <span class="nav-text">泛型方法的示例</span></a></li></ol></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
